<template>
  <!-- 定位卡管理 -->
  <div class="locationcard">
    <basic-container>
      <avue-crud
        :option="option"
        :table-loading="loading"
        :data="data"
        :page.sync="page"
        :permission="permissionList"
        :before-open="beforeOpen"
        v-model="form"
        ref="crud"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
        @search-change="searchChange"
        @search-reset="searchReset"
        @selection-change="selectionChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
      >
        <!-- 左上方菜单内容 -->
        <template slot="menuLeft">
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            plain
            v-if="permission.locationcardinfo_delete"
            @click="handleDelete"
          >
            删 除
          </el-button>
        </template>

        <!-- 电量表格自定义 -->
        <template slot="dl" slot-scope="scope">
          <div>
            <span
              v-if="
                scope.row['dl'] === '' ||
                  scope.row['dl'] === undefined ||
                  scope.row['dl'] < 0
              "
              >-</span
            >
            <span v-else>{{ scope.row["dl"] }}%</span>
          </div>
        </template>

        <!-- 是否在线表格自定义 -->
        <template slot="sfzx" slot-scope="scope">
          <div>
            <span>{{ onlineObj[scope.row["sfzx"]] || "-" }}</span>
          </div>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {
  getList,
  getDetail,
  add,
  update,
  remove
} from "@/api/useronduty/locationcard";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      form: {},
      query: {},
      data: [],
      loading: true,
      selectionList: [],
      page: {
        pageSize: this.$pageSize,
        currentPage: 1,
        total: 0
      },
      option: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchSpan: 5,
        searchMenuSpan: 4,
        border: true,
        viewBtn: true,
        selection: true,
        refreshBtn: true,
        columnBtn: false,
        dialogClickModal: false,
        column: [
          {
            label: "标签号",
            prop: "bqh",
            align: "center",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入标签号",
                trigger: "blur"
              }
            ]
          },
          {
            label: "定位卡号",
            prop: "dwkh",
            align: "center",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入定位卡号",
                trigger: "blur"
              }
            ]
          },
          {
            label: "载入时间",
            prop: "zrsj",
            align: "center",
            display: false
          },
          {
            label: "更新时间",
            prop: "gxsj",
            align: "center",
            display: false
          },
          {
            label: "电量",
            prop: "dl",
            align: "center",
            display: false,
            slot: true
          },
          {
            label: "是否在线",
            prop: "sfzx",
            align: "center",
            display: false,
            search: true,
            type: "select",
            slot: true,
            dicData: [
              {
                label: "离线",
                value: 0
              },
              {
                label: "在线",
                value: 1
              }
            ]
          }
        ]
      },
      onlineObj: {
        0: "离线",
        1: "在线"
      }
    };
  },
  computed: {
    ...mapGetters(["permission"]),

    // 定位卡列表按钮权限
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.locationcardinfo_add, false),
        viewBtn: this.vaildData(this.permission.locationcardinfo_view, false),
        delBtn: this.vaildData(this.permission.locationcardinfo_delete, false),
        editBtn: this.vaildData(this.permission.locationcardinfo_edit, false)
      };
    },

    // 定位卡列表勾选项ID
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  mounted() {
    this.onLoad(this.page);
  },
  methods: {
    // 新增
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message.success("操作成功!");
          done();
        },
        error => {
          loading();
          window.console.log(error);
        }
      );
    },

    // 修改
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message.success("操作成功!");
          done();
        },
        error => {
          loading();
          console.log(error);
        }
      );
    },

    // 删除一条
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message.success("操作成功!");
        });
    },

    // 删除多条
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message.success("操作成功!");
          this.$refs.crud.toggleSelection();
        });
    },

    // 打开查看、新增、编辑弹窗前
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },

    // 清空搜索条件
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },

    // 搜索条件改变事件
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },

    // 勾选
    selectionChange(list) {
      this.selectionList = list;
    },

    // 清空勾选
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },

    // 换页数
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },

    // 换每页显示数量
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },

    // 刷新
    refreshChange() {
      this.onLoad(this.page, this.query);
    },

    // 获取表格数据
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style lang="scss" scpoed>
.locationcard {
  .avue-crud {
    .avue-crud__search {
      .avue-form__row:nth-child(1) {
        .el-form-item__label {
          text-align: unset;
        }
      }
    }
  }
}
</style>